﻿using assignment2.Entity;
using assignment2.Events;
using assignment2.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace assignment2
{
    public partial class Form2 : Form
    {
        public CreditAccount CurrentAccount { get; set; }
        private ATM atm = new ATM();
        private Account account=new Account();
        private List<CreditAccount> creditAccouts = new List<CreditAccount>();
        public Form2()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form2_Load);
            atm.BigMoneyEvent += overMax;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            label2.Text = CurrentAccount.ID;
            label4.Text = CurrentAccount.Balance.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label6.Visible = true;
            textBox1.Visible = true;
            button3.Visible = true;
            label7.Visible = false;
            textBox2.Visible = false;
            button4.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label7.Visible = true;
            textBox2.Visible = true;
            button4.Visible = true;
            label6.Visible = false;
            textBox1.Visible = false;
            button3.Visible = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            double amount = Convert.ToDouble(textBox1.Text);
            try
            {
                CurrentAccount.Deposit(amount);
                MessageBox.Show("存款成功,当前余额为：" +CurrentAccount.Balance);
                label4.Text = CurrentAccount.Balance.ToString();
            }
            catch (BadCashException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            double amount = Convert.ToDouble(textBox2.Text);
            try
            {
                atm.Withdraw(amount, CurrentAccount);
                MessageBox.Show("取款成功，当前余额为" + CurrentAccount.GetBalance());
                label4.Text = CurrentAccount.Balance.ToString();
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message);

            }
            
        }

        private void overMax(BigMoneyArgs bigMoneyArgs)
        {
            string user = bigMoneyArgs.creditAccount.ID;
            double amount = bigMoneyArgs.balance;
            MessageBox.Show($"取款金额超过10000元,为{amount}元");

        }
    }
}
